using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Reflection;
using System.Text;
using WMS.Api;
using WMS.Infrastructure;
using Yitter.IdGenerator;

var builder = WebApplication.CreateBuilder(args);

//授权认证（使用JWT）
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o =>
{
    // 私钥
    var secretByte = Encoding.UTF8.GetBytes("kdsfldsflkdslkflkdsflkdslfdslkflk");
    o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
    {
        // 验证发布者
        ValidateIssuer = true,
        // 发布者信息
        ValidIssuer = "BeiJingBW",

        // 验证接收者
        ValidateAudience = true,
        // 接收者
        ValidAudience = "BeiJingBW",

        // 验证是否过期
        ValidateLifetime = true,

        // 验证私钥
        IssuerSigningKey = new SymmetricSecurityKey(secretByte)

    };
});

// 添加服务到容器
builder.Services.AddControllers();
// 配置Swagger/OpenAPI
builder.Services.AddEndpointsApiExplorer();

// 配置Swagger
builder.Services.AddSwaggerGen(
    o =>
    {
        //swagger加中文注释   在api右击选择属性 --- 生成 --- 输出--勾上xml文档
        //string xmlComment = AppDomain.CurrentDomain.BaseDirectory + "Hotel.Api.xml";
        //o.IncludeXmlComments(xmlComment, true);

        // 显示 授权信息
        o.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme()
        {
            Description = "添加JWT授权Token：Bearer Token值",
            Name = "Authorization",
            In = Microsoft.OpenApi.Models.ParameterLocation.Header,
            Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
            BearerFormat = "JWT",
            Scheme = "Bearer"
        });
        o.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
            },
            new string[]
            {

            }
        }
    });
    });

// 配置AutoMapper
builder.Services.AddAutoMapper(typeof(MappingProfiles));

// 配置雪花ID生成器
YitIdHelper.SetIdGenerator(new IdGeneratorOptions());

// 配置数据库连接
builder.Services.AddDbContext<MyDBContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});

// 注册依赖注入服务
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));

//注册中介者
builder.Services.AddMediatR(config => config.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

var app = builder.Build();

// 配置HTTP请求管道
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}



// 配置CORS
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());


// 鉴权
app.UseAuthentication();

// 授权
app.UseAuthorization();

app.MapControllers();

// 启用静态文件服务
app.UseStaticFiles();

app.Run();
